package cn.yjr.vector;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @author vector
 *
 */
public class GuidePageActivity extends Activity implements OnTouchListener{
	private ViewPager mViewPager;
	private PagerTitleStrip mPagerTitleStrip;
	private ImageView imageView;
	private int curIndex = 0;
	private int lastX = 0;
	private ArrayList<View> pageViews;
	private ViewGroup guidePage,group;
	private ImageView[] imageViews;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		LayoutInflater inflater = getLayoutInflater(); 
		
		pageViews = new ArrayList<View>();
		pageViews.add(inflater.inflate(R.layout.guide_page_item0, null));
		pageViews.add(inflater.inflate(R.layout.guide_page_item1, null));
		pageViews.add(inflater.inflate(R.layout.guide_page_item2, null));
		pageViews.add(inflater.inflate(R.layout.guide_page_item3, null));
		
		imageViews = new ImageView[pageViews.size()];
		
		guidePage = (ViewGroup) inflater.inflate(R.layout.activity_guide_page, null);
		// group是R.layou.main中的负责包裹小圆点的LinearLayout. 
		group = (ViewGroup)guidePage.findViewById(R.id.viewGroup);
		mViewPager = (ViewPager) guidePage.findViewById(R.id.guide_pages);
		mViewPager.setOnTouchListener(this);
		
		//显示标题，未用到
		mPagerTitleStrip = (PagerTitleStrip) guidePage.findViewById(R.id.pagertitle);
		
		for(int i = 0; i < pageViews.size(); i++){
			imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(40,40));
			//imageView.setPadding(20, 0, 20, 0);
			imageViews[i] = imageView;
			if(i==0){
				//默认选中第一张图片
				imageViews[i].setBackgroundResource(R.drawable.page_focused);
			}else{
				imageViews[i].setBackgroundResource(R.drawable.page_unfocused);
			}
			group.addView(imageViews[i]);
		}
		setContentView(guidePage);
		
		mViewPager.setAdapter(new GuidePageAdapter());
		mViewPager.setOnPageChangeListener(new GuidePageChangeListener());
		
	}
	
	class GuidePageAdapter extends PagerAdapter{

		/* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#getCount()
		 */
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return pageViews.size();
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#isViewFromObject(android.view.View, java.lang.Object)
		 */
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0==arg1;
		}
		
		@Override
		public int getItemPosition(Object object){
			return super.getItemPosition(object);
		}
		
		@Override 
		public void destroyItem(View arg0, int arg1, Object arg2) { 
			((ViewPager) arg0).removeView(pageViews.get(arg1)); 
		} 
		
		@Override
		public Object instantiateItem(View arg0, int arg1){
			((ViewPager) arg0).addView(pageViews.get(arg1));
			return pageViews.get(arg1);
		}
		
		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1){
			// TODO Auto-generated method stub
		}
		
		@Override 
		public Parcelable saveState() { 
			// TODO Auto-generated method stub 
			return null; 
		} 
		
		@Override
		public void startUpdate(View arg0) { 
			// TODO Auto-generated method stub 
		} 
		@Override 
		public void finishUpdate(View arg0) { 
			// TODO Auto-generated method stub 
		}
	}
	
	class GuidePageChangeListener implements OnPageChangeListener{
		/**
		 * 这3个方法的调用时机各不一样。其中，onPageScrollStateChanged()方法发生在滚动状态变化时，
		 * 用于监测用户开始拖动ViewPager；onPageScrolled()方法发生在当前页面滚动时，用于监测用于拖
		 * 动当前页面的程度（偏移量多少）；onPageSelected()方法发生在当前页面被选择时，用于监测当前滑动到了哪一个页面。
		 */
		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			curIndex = arg0;
			for(int i =0; i < imageViews.length; i++){
				imageViews[i].setBackgroundResource(R.drawable.page_unfocused); 
			}
			imageViews[arg0].setBackgroundResource(R.drawable.page_focused);
		}
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			lastX = (int)event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			if((lastX - event.getX()) >100 && (curIndex == imageViews.length -1)){
				Intent intent= new Intent(GuidePageActivity.this, TransactionRecordActivity.class);
				startActivity(intent);
				this.finish();
			}
			break;
		default:
			break;
		}
		return false;
	}
}
